package com.td.framework.kqxt.jbpm.action;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.model.ActivityCoordinates;

import com.td.framework.common.AbstractAction;
import com.td.framework.kqxt.domain.KqBcsz;
import com.td.framework.utils.JBPMUtil;
import com.td.framework.webmanager.page.PageService;

/**
 * 流程管理流程的增删改查信息
 * @author Administrator
 */
public class ProcessInfoAction extends AbstractAction {
	private  File file;//文件上传
    private String fileContentType; 
    private String fileFileName;//文件的名横
    private String deploymentId;//流程部署id
    private List<ProcessDefinition> processList;//所有流程列表
    private String instanceId;//流程实例id
    private String picPath ="/kqxt/kqxx/bqxx/viewBqProcess.jsp";
    private ActivityCoordinates ac;
    
    private JBPMUtil jbpmUtil;
    Log log = LogFactory.getLog(ProcessInfoAction.class);

    /**
     * 流程列表
     * @return
     */
    public String listProcess(){
    	
    	ProcessEngine processEngine = jbpmUtil.getProcessEngine();
    	processList = processEngine.getRepositoryService().createProcessDefinitionQuery().list();    	
    	int rows = processList.size();				
		page = PageService.getPage(rows, currentPage, pageSize, pageMethod);
		this.setCurrentPage(page.getCurentPage());		
		this.setPageSize(page.getPageSize());		
		return "listProcess";
    }
    
    /**
     * 打开流程添加页面
     */
    public String createProcessForm(){
    	this.setEditState(1);
    	return "createProcess";
    }
    
    /**
     * 添加流程信息
     * @return
     */
    public String createProcess(){
	   boolean flag = true;
		try {			
	    	ProcessEngine processEngine = jbpmUtil.getProcessEngine();
	    	
	    	processEngine.getRepositoryService().createDeployment().addResourcesFromZipInputStream(new ZipInputStream(new FileInputStream(file))).deploy();
		} catch (Exception e) {
			 log.error("添加流程信息失败",e);
			 this.handleMessage("info", "添加失败,联系管理员!");
		}
		if(flag = true){
	    	 this.handleMessage("info", "添加成功，请继续操作!");
	     }
		this.setEditState(1);
	     return "createProcess";
    }
    
    /**
     * 删除选中流程的信息
     * @return
     */
    public String removeProcess(){
    	 boolean flag = false;
  	     
	    ProcessEngine processEngine = jbpmUtil.getProcessEngine();
	    processEngine.getRepositoryService().deleteDeploymentCascade(deploymentId);
  	     return "listProcess"; 
    }
    
    /**
     *打开流程设置展现页面
     * @return
     */
    public String showProcess(){
    	this.setEditState(2);
    	//kqProcess = (KqProcess) this.getBaseService().getEntityById(KqProcess.class,zids[0]);
    	return "showProcess";
    }

    /**
     * 查看流程实例图片信息
     */
    public String showPic(){    	    	
    	ProcessInstance processInstance = jbpmUtil.getExecutionService().findProcessInstanceById("20001");
    	Set<String> activityNames = processInstance.findActiveActivityNames();	
    	ac = jbpmUtil.getRepositoryService().getActivityCoordinates(processInstance.getProcessDefinitionId(),activityNames.iterator().next());
    	return "showPic";
    }
    
	public JBPMUtil getJbpmUtil() {
		return jbpmUtil;
	}

	public void setJbpmUtil(JBPMUtil jbpmUtil) {
		this.jbpmUtil = jbpmUtil;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getDeploymentId() {
		return deploymentId;
	}

	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}

	public List<ProcessDefinition> getProcessList() {
		return processList;
	}

	public void setProcessList(List<ProcessDefinition> processList) {
		this.processList = processList;
	}

	public String getInstanceId() {
		return instanceId;
	}

	public void setInstanceId(String instanceId) {
		this.instanceId = instanceId;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public ActivityCoordinates getAc() {
		return ac;
	}

	public void setAc(ActivityCoordinates ac) {
		this.ac = ac;
	}    
	
}
